package com.zego.wawaji.ui.Fragment.web;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zego.wawaji.R;
import com.zego.wawaji.base.bean.Common;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.noble.library.observer.PObserver;
import com.zego.wawaji.ui.Fragment.BaseFragment;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.activity.web.WebPanel;

/**
 * 充值排行榜
 * Created by Su on 2018/1/18.
 */
public class RankFragment extends BaseFragment implements View.OnClickListener, PObserver {
    private WebPanel webPanel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.rank_fragment);

        initTitle();
        initView();

        return getContentView();
    }

    private void initTitle() {
        setTitle("限时活动");
        setTitleRight("奖品", v -> UIShow.showMyPrizeActivity(getActivity()));
    }

    private void initView() {
        MsgMgr.getInstance().attach(this);
        LinearLayout web_container = (LinearLayout) findViewById(R.id.web_container);

//        String activityUrl = "http://invite.yanyuan01.cn/overwrite_activities.html";
        String activityUrl = "http://invite.dmwawa.com/limit-activities.html";
        Common common = ModuleMgr.getWawaMgr().getCommon();
        if (!TextUtils.isEmpty(common.getActivityUrl())) {
            activityUrl = common.getActivityUrl();
        }

        webPanel = new WebPanel(getActivity(), activityUrl + "?time=" + ModuleMgr.getAppMgr().getTime(), true);
        web_container.removeAllViews();
        web_container.addView(webPanel.getContentView());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }

    private void refreshWeb() {
        if (webPanel != null) {
            webPanel.resetWebView(true);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            refreshWeb();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (webPanel != null) {
            webPanel.getWebView().onResume();
            webPanel.getWebView().resumeTimers();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (webPanel != null) {
            webPanel.getWebView().onPause();
            webPanel.getWebView().pauseTimers();
        }
    }

    @Override
    public void onDestroy() {
        if (webPanel != null) webPanel.onDestroy();
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Web_Refresh:
                refreshWeb();
                break;
        }
    }
}
